package ThreadTaskUtil;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Component
@Configuration
public class ThreadTaskPoolUtil {

    public Boolean threadMethodUtil(Object targetObject,String methodName,Integer maxThread) throws InterruptedException, InvocationTargetException, IllegalAccessException {

        Method[] targetObjectMethodList = targetObject.getClass().getMethods();
        Method method = null;
        for (Method methodItem : targetObjectMethodList) {
            if(methodItem.getName().equals(methodName)){
                method = methodItem;
            }
        }

        CountDownLatch countDownLatch = new CountDownLatch(maxThread);
        ExecutorService executorService = Executors.newFixedThreadPool(maxThread);

        long start = System.currentTimeMillis();
        for (int i = 0; i < maxThread; i++) {
            Method finalMethod = method;
            executorService.submit(() -> {
                try {
                    try {
                        finalMethod.invoke(targetObject,Thread.currentThread().getName());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                } finally {
                    System.out.println("finally");
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await(5000, TimeUnit.SECONDS);
        System.out.println("任务执行完毕，继续执行主线程任务。\n耗时ms：" + (System.currentTimeMillis() - start));
        // 不要忘记关闭线程池，不然会导致主线程阻塞无法退出
        executorService.shutdown();
        return true;
    }

    public Boolean threadListUtil(CopyOnWriteArrayList<Object> targetList, Integer splitListFlag,Integer maxThread) throws InterruptedException, InvocationTargetException, IllegalAccessException {

   /*     ReentrantLock reentrantLock = new ReentrantLock();


     *//*   //归一化为线程安全的List
        try{
            reentrantLock.lock();
            targetList.forEach(item -> {
                objectCopyOnWriteArrayList.add(item);
            });
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }*//*


        //分割类型，指定多少组元素为一个执行线程


        CountDownLatch countDownLatch = new CountDownLatch(maxThread);
        ExecutorService executorService = Executors.newFixedThreadPool(maxThread);


        for (int i = 0; i < targetList.size(); i=i+splitListFlag) {
            CopyOnWriteArrayList<Object> taskItemList = new CopyOnWriteArrayList<>();
            for (int j = 0; j < splitListFlag; j++) {

            }
            splitListFlag = i;
            taskItemList.add()
        }


        long start = System.currentTimeMillis();
        for (int i = 0; i < maxThread; i++) {
            Method finalMethod = method;
            executorService.submit(() -> {
                try {
                    try {
                        finalMethod.invoke(targetObject,Thread.currentThread().getName());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                } finally {
                    System.out.println("finally");
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await(5000, TimeUnit.SECONDS);
        System.out.println("任务执行完毕，继续执行主线程任务。\n耗时ms：" + (System.currentTimeMillis() - start));
        // 不要忘记关闭线程池，不然会导致主线程阻塞无法退出
        executorService.shutdown();*/
        return true;
    }

}
